﻿Ext.data.RestProxy = Ext.extend(Ext.data.AjaxProxy, {

    appendId: true,

    actionMethods: {
        create: 'POST',
        read: 'GET',
        update: 'PUT',
        destroy: 'DELETE'
    },

    api: {
        create: 'create',
        read: 'read',
        update: 'update',
        destroy: 'destroy'
    },

    // 组装url
    buildUrl: function (request) {
        var records = request.operation.records || [],
            record = records[0],
            format = this.format,
            url = request.url || this.url;

        if (this.appendId && record) {
            if (!url.match(/\/$/)) {
                url += '/';
            }

            url += record.getId(); // 将url组装成以'/xxx'结尾的形式
        }

        // 如果指定后缀名，加上后缀名
        if (format) {
            if (!url.match(/\.$/)) {
                url += '.';
            }

            url += format;
        }

        request.url = url;

        return Ext.data.RestProxy.superclass.buildUrl.apply(this, arguments);
    }
});

Ext.data.ProxyMgr.registerType('rest', Ext.data.RestProxy);